package cn.edu.ujn.finaltest.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.mybatis.generator.internal.util.StringUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.finaltest.dao.BaseDict;
import cn.edu.ujn.finaltest.dao.OrderItem;
import cn.edu.ujn.finaltest.dao.OrderItemMapper;
import cn.edu.ujn.finaltest.dao.OrdersMapper;
import cn.edu.ujn.finaltest.util.Page;

@Service
@Transactional
public class OrderItemServiceImpl implements IOrderItemService {

	@Autowired
	OrderItemMapper orderItemDao;
	@Autowired
	OrdersMapper ordersDao;
	@Override
	public int deleteByPrimaryKey(int id) {
		// TODO Auto-generated method stub
		int deleteByPrimaryKey = orderItemDao.deleteByPrimaryKey(id);
		return deleteByPrimaryKey;
	}
	
	@Override
	public int insert(OrderItem row) {
		// TODO Auto-generated method stub
		int insert = orderItemDao.insert(row);
		return insert;
	}

	@Override
	public int insertSelective(OrderItem row) {
		// TODO Auto-generated method stub
		int insertSelective = orderItemDao.insertSelective(row);
		return insertSelective;
	}

	@Override
	public OrderItem selectByPrimaryKey(int id) {
		// TODO Auto-generated method stub
		OrderItem selectByPrimaryKey = orderItemDao.selectByPrimaryKey(id);
		return selectByPrimaryKey;
	}

	@Override
	public int updateByPrimaryKeySelective(OrderItem row) {
		// TODO Auto-generated method stub
		int updateByPrimaryKey = orderItemDao.updateByPrimaryKey(row);
		return updateByPrimaryKey;
	}

	@Override
	public int updateByPrimaryKey(OrderItem row) {
		// TODO Auto-generated method stub
		int updateByPrimaryKey = orderItemDao.updateByPrimaryKey(row);
		return updateByPrimaryKey;
	}

	@Override
	public int deleteByOrderId(int orderId) {
		// TODO Auto-generated method stub
		int deleteNums = orderItemDao.deleteByOrderId(orderId);
		return deleteNums;
	}

	@Override
	public List<OrderItem> selectOrderItemList(OrderItem orderItem) {
		// TODO Auto-generated method stub
		List<OrderItem> selectOrderItemList = orderItemDao.selectOrderItemList(orderItem);
		return selectOrderItemList;
	}
	
	@Override
	public List<OrderItem> selectUserOrderItemList(OrderItem orderItem) {
		// TODO Auto-generated method stub
		List<OrderItem> selectUserOrderItemList = this.orderItemDao.selectUserOrderItemList(orderItem);
		return selectUserOrderItemList;
	}

	@Override
	public Page<OrderItem> findOrderItemList(Integer page, Integer rows, OrderItem selectParams) {
		// TODO Auto-generated method stub
		OrderItem orderItem = new OrderItem();
		Integer userID = selectParams.getUser_id();
		Integer productId = selectParams.getProduct_id();
		Integer orderId = selectParams.getOrder_id();
		String productName = selectParams.getProduct_name();
		String orderNumber = selectParams.getOrder_number();
		// 设置查询条件
		if (userID != null) {
			orderItem.setUser_id(userID);
		}
		if (productId != null) {
			orderItem.setProduct_id(productId);
		}
		if (orderId != null) {
			orderItem.setOrder_id(orderId);
		}
		if(!StringUtils.isNullOrEmpty(productName)) {
			orderItem.setProduct_name(productName);
		}
		if(!StringUtils.isNullOrEmpty(orderNumber)) {
			orderItem.setOrder_number(orderNumber);
		}
		// 查询列表总记录数
		//Integer count = selectOrderItemList.size();
		Integer count = this.orderItemDao.selectUserOrderItemList(orderItem).size();
		// 设置查询结果开始行数
		orderItem.setStart((page - 1) * rows);
		// 设置查询结果返回条数
		orderItem.setRows(rows);
		// 执行分页查询
		List<OrderItem> selectOrderItemList = this.orderItemDao.selectUserOrderItemList(orderItem);
		// 创建Page返回对象
		Page<OrderItem> result = new Page<OrderItem>();
		// 结果为page页、rows行，数据集为baseDicts，总共count条数据
		result.setPage(page);
		result.setRows(selectOrderItemList);
		result.setSize(rows);
		result.setTotal(count);
		return result;
	}

	@Override
	public int insertSelectiveNums(OrderItem orderItem, int nums) {
		// TODO Auto-generated method stub
		int count = 0; 						// 成功插入的订单项计数
		OrderItem insertOrderItem = new OrderItem();
		insertOrderItem.setOrder_id(orderItem.getOrder_id());
		insertOrderItem.setProduct_id(orderItem.getProduct_id());
		for (int i = 0; i < nums; i++) {
			orderItemDao.insertSelective(orderItem);
			count++;
		}
		return count;
	}

	@Override
	public int deleteOrderItemsNums(OrderItem oriderItem, int nums) {
		// TODO Auto-generated method stub
		int count = 0;	// 删除数据项计数
		// 获取所有需要删除的订单项id
		List<Integer> deleteOrderItemsId = new ArrayList<Integer>();
		OrderItem deleteOrderItemParam = new OrderItem();
		deleteOrderItemParam.setOrder_id(oriderItem.getOrder_id());
		deleteOrderItemParam.setProduct_id(oriderItem.getProduct_id());
		List<OrderItem> selectOrderItemList = selectOrderItemList(deleteOrderItemParam);
		for (OrderItem deleteOrderItem : selectOrderItemList) {
			deleteOrderItemsId.add(deleteOrderItem.getId());
		}
		for (Integer deleteId : deleteOrderItemsId) {
			if (count < nums) {
				this.orderItemDao.deleteByPrimaryKey(deleteId);
				count++;
			} else {
				break;
			}
		}
		// 订单项删除完毕，检查订单是否为空，若订单为空，则删除对应订单
		OrderItem checkEmpty = new OrderItem();
		checkEmpty.setOrder_id(oriderItem.getOrder_id());
		List<OrderItem> orderItemList = this.orderItemDao.selectOrderItemList(checkEmpty);
		if (orderItemList.size() == 0) {
			// 如果orders对应的orderitem全部删除，则删除这个orders
			this.ordersDao.deleteByPrimaryKey(oriderItem.getOrder_id());
		}
		return count;
	}

}
